package gae.google.tcserver.servlets;

import gae.google.tcserver.db.SaveToDB;
import gae.google.tcserver.db.TeamMember;

import java.io.IOException;
//import java.util.logging.Logger;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;


/**
 * The <tt>CreateTeamMemberServlet</tt> class creates a new team
 * up on received team member input data. 
 *
 * @author Lars Friberg
 *
 * @date 2011-01-20
 */
public class CreateTeamMemberServlet extends HttpServlet {

	
	//private static final Logger log = Logger.getLogger(LoginServlet.class.getName());


	/**
	 * serial version
	 */
	private static final long serialVersionUID = -3779213583873700949L;

	/* (non-Javadoc)
	 * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		//final Logger log = Logger.getLogger(LoginServlet.class.getName());

		// save all parameters received from CreateTeam.jsp
		String teamId = request.getParameter("teamId");
		String teamMemberFirstName = request.getParameter("teamMemberFirstName");
		String teamMemberFamilyName = request.getParameter("teamMemberFamilyName");
		String teamMemberAddress = request.getParameter("teamMemberAddress");
		String teamMemberZipCode = request.getParameter("teamMemberZipCode");
		String teamMemberLocation = request.getParameter("teamMemberLocation");
		String teamMemberPhoneNumber = request.getParameter("teamMemberPhoneNumber");
		String teamMemberMailAddress = request.getParameter("teamMemberMailAddress");
		String teamMemberRole = request.getParameter("teamMemberRole");
		String teamMemberImageName = request.getParameter("teamImageName");
		
		// convert team id from String to Key
		Key teamKeyId = KeyFactory.stringToKey(teamId);

		// create team member
		TeamMember teamMember = new TeamMember(teamMemberFirstName, teamMemberFamilyName, teamMemberAddress,
											   teamMemberZipCode, teamMemberLocation, teamMemberPhoneNumber,
											   teamMemberMailAddress, teamMemberRole, teamMemberImageName, teamKeyId);
		// save team in DB
		SaveToDB.saveTeamMember(teamMember);
		SaveToDB.saveTeamMemberKeyId(teamKeyId, teamMember.getKeyId());
		
		// dispatch to TeamMembers.jsp
		String url = "/views/TeamMembers.jsp?id=" + teamId;
		RequestDispatcher view = getServletContext().getRequestDispatcher(url);
		view.forward(request, response);
	}
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException
	{
		// dispatch to TeamMembers.jsp
		RequestDispatcher view = getServletContext().getRequestDispatcher("/views/TeamMembers.jsp");
		view.forward(request, response);
	}
}
